package club.ximeng.sliceupload.common.config;

import club.ximeng.sliceupload.common.constants.FileConstant;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author liu xm
 * @date 2022-03-07 14:01
 */
@Configuration
public class CustomWebConfiguration implements WebMvcConfigurer {

    /**
     * springboot 2.6.x  addResourceHandlers 失效
     *
     * @param registry 资源处理程序的注册
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/file/**")
                .addResourceLocations("file:" + FileConstant.FILE_MERGE_PATH);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT")
                .maxAge(3600);
    }

}
